package com.polaris.live.adapter.backpack

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.polaris.live.R
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.databinding.ItemBackpackToolDiscountBinding
import com.polaris.live.databinding.ItemBackpackToolNoDiscountBinding
import com.polaris.live.resp.back_resp.UserBackpackConsumableResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder
import java.util.concurrent.TimeUnit

/**
 * BackPackDiscountAdapter  折扣adapter
 *
 * @author Created by 半仙 on 2024/1/31/031 11:44
 */
class BackPackDiscountAdapter : BaseQuickAdapter<Any, BaseBackPackDiscountHolder<*, Any>>(0) {

    init {
        addChildClickViewIds(R.id.tv_chick1)
    }

    override fun getDefItemViewType(position: Int): Int {
        return when (data[position]) {
            is String -> NOT_USER
            is UserBackpackConsumableResp -> COMMON
            else -> throw Exception("no type")
        }

    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBackPackDiscountHolder<*, Any> {
        return when (viewType) {
            NOT_USER -> {
                NoUserHolder(
                    ItemBackpackToolNoDiscountBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            COMMON -> {
                ItemHolder(
                    ItemBackpackToolDiscountBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            else -> {
                ItemHolder(
                    ItemBackpackToolDiscountBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }
        }
    }

    override fun convert(holder: BaseBackPackDiscountHolder<*, Any>, item: Any) {
        holder.bind(item)
    }

    companion object {

        const val NOT_USER = 1
        const val COMMON = 2
    }

    class NoUserHolder(binding: ItemBackpackToolNoDiscountBinding) :
        BaseBackPackDiscountHolder<ItemBackpackToolNoDiscountBinding, Any>(binding) {

        override fun bind(item: Any) {

        }
    }

    class ItemHolder(binding: ItemBackpackToolDiscountBinding) :
        BaseBackPackDiscountHolder<ItemBackpackToolDiscountBinding, Any>(binding) {

        override fun bind(item: Any) {
            val data = item as UserBackpackConsumableResp
            mBinding.apply {
                Glide.with(icImage).load(ImageUtils.imgPath(data.displayUrl)).into(icImage)
                setTime(data)
                tvName.text = data.name
                if (item.amount > 1) {
                    tvCount.visibility = View.VISIBLE
                    tvCount.text = "x${item.amount}"
                } else {
                    tvCount.visibility = View.GONE
                }
            }
        }

        private fun setTime(item: UserBackpackConsumableResp) {
            mBinding.apply {
                val expiredTime = item.expiredTime
                if (expiredTime.isNullOrEmpty()) {
                    ivTimeIcon.visibility = View.GONE
                    tvTime.text = StringUtils.getString(string.permanent)
                } else {
                    ivTimeIcon.visibility = View.VISIBLE
                    item.expireDays?.let {
                        if (it == 0L) {
                            tvTime.text = formatTime(expiredTime.toLong() - CacheUtil.getServerTime())
                        } else {
                            val (day, num) = if (it < 365) {
                                "day" to it
                            } else {
                                "year" to it / 365
                            }
                            tvTime.text = "$num$day"
                        }
                    }
                }
            }
        }
    }
}

abstract class BaseBackPackDiscountHolder<VB : ViewBinding, T>(binding: VB) :
    BaseBindingViewHolder<VB, T>(binding) {

    fun formatTime(timeInMillis: Long): String {
        val days = TimeUnit.MILLISECONDS.toDays(timeInMillis)
        val hours = TimeUnit.MILLISECONDS.toHours(timeInMillis)
        val minutes = TimeUnit.MILLISECONDS.toMinutes(timeInMillis)
        val seconds = TimeUnit.MILLISECONDS.toSeconds(timeInMillis)
        return when {
            days > 0 -> StringUtils.getString(string.days, days)
            hours > 0 -> StringUtils.getString(string.hours, hours)
            minutes > 0 -> StringUtils.getString(string.minutes, minutes)
            seconds > 0 -> StringUtils.getString(string.seconds, seconds)
            else -> StringUtils.getString(string.expired)
        }
    }
}